/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tces.view;
//
//import com.cashmate.model.*;
//import com.cashmate.controller.*;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.tces.controller.StudentDAO;
import com.tces.model.Students;
import java.util.Map;
import java.util.regex.Pattern;
import org.apache.log4j.Logger;

public class AuthenticationAction extends ActionSupport {
//

    private static final Logger log = Logger.getLogger(AuthenticationAction.class);
    private String studentIDNumber, studentPassword, studentNewPassword, studentConfirmPassword;

    @Override
    public String execute() {
        try {
            clearActionErrors();
            Students authenticatedUser;
            log.info("ID Number : " + studentIDNumber + " Password : " + studentIDNumber.trim().length());
            if (!Pattern.matches("\\d{2}-\\d{6}", studentIDNumber)) {
                addActionError("Invalid ID Number format.");
                log.error("Invalid ID Number format.");
            }
            if (studentPassword.trim().length() <= 7 || studentPassword.trim().length() > 32) {
                addActionError("Invalid password length. Password must be 8 - 32 characters.");
                log.error("Invalid password length. Password must be 8 - 32 characters.");
            }
            if (hasActionErrors()) {
                return INPUT;
            }
            clearActionErrors();
            authenticatedUser = StudentDAO.authenticate(studentIDNumber, studentPassword);
            if (authenticatedUser == null) {
                addActionError("ID Number and/or Password is incorrect. Please try again!");
                return INPUT;
            }
            //        HttpSession session = ServletActionContext.getRequest().getSession(true);
            Map session = ActionContext.getContext().getSession();
            session.put("student", authenticatedUser);

            log.info(authenticatedUser.getStudentFirstName() + " " + authenticatedUser.getStudentLastName() + " of " + authenticatedUser.getCollegeCode() + " is now online");
            ActionContext.getContext().setSession(session);

        } catch (Exception ex) {
            log.error(ex);
        }
        return SUCCESS;
    }

    public String logout() {
        try {
            Map session = ActionContext.getContext().getSession();
            session.remove("student");

        } catch (Exception ex) {
            log.error(ex);
        }
        return SUCCESS;
    }

    public String saveLoginCredential() {
        try {
            Students student;

            clearActionErrors();
            student = (Students) ActionContext.getContext().getSession().get("student");
            if (!student.getStudentIDNumber().equals(studentIDNumber)) {
                addActionError("Changing password of the other is not allowed. This incident has been reported.");
                log.error("Changing password of the other is not allowed. This incident has been reported.");
            }
            log.info("ID Number : " + studentIDNumber + " Password : " + studentIDNumber.trim().length());
            if (!Pattern.matches("\\d{2}-\\d{6}", studentIDNumber)) {
                addActionError("Invalid ID Number format.");
                log.debug("Invalid ID Number format.");
            }
            if (studentPassword.trim().length() <= 7 || studentPassword.trim().length() > 32) {
                addActionError("Invalid password length. Password must be 8 - 32 characters.");
                log.debug("Invalid password length. Password must be 8 - 32 characters.");
            }
            if (studentNewPassword.trim().length() <= 7 || studentNewPassword.trim().length() > 32) {
                addActionError("Invalid new password length. Password must be 8 - 32 characters.");
                log.debug("Invalid password length. Password must be 8 - 32 characters.");
            }
            if (!studentNewPassword.equals(studentConfirmPassword)) {
                log.debug("Password doesn't match.");
                addActionError("Password doesn't match.");
            }
            if (hasActionErrors()) {
                return INPUT;
            }
            clearActionErrors();
            student=null;
            log.info("ID Number : " + studentIDNumber + " Password : " + studentPassword);
            student = StudentDAO.authenticate(studentIDNumber, studentPassword);
            if (student == null) {
                addActionError("ID Number and/or Password is incorrect. Please try again!");
                return INPUT;
            }
            if (StudentDAO.savePassword(studentIDNumber, studentNewPassword) > 0) {
                addActionMessage("Successfully saved password. Please logout then login again to apply the changes");
            } else {
                addActionError("Unable to save new password. Please check the logfile for more info.");
                return INPUT;
            }

        } catch (Exception ex) {
            log.error(ex);
        }
        return SUCCESS;
    }

    public String getStudentIDNumber() {
        return studentIDNumber;
    }

    public void setStudentIDNumber(String studentIDNumber) {
        this.studentIDNumber = studentIDNumber;
    }

    public String getStudentPassword() {
        return studentPassword;
    }

    public void setStudentPassword(String studentPassword) {
        this.studentPassword = studentPassword;
    }

    public String getStudentConfirmPassword() {
        return studentConfirmPassword;
    }

    public void setStudentConfirmPassword(String studentConfirmPassword) {
        this.studentConfirmPassword = studentConfirmPassword;
    }

    public String getStudentNewPassword() {
        return studentNewPassword;
    }

    public void setStudentNewPassword(String studentNewPassword) {
        this.studentNewPassword = studentNewPassword;
    }
}
